﻿using FoundationLib.Common.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FoundationLib.Common.Extensions
{
	/// <summary>
	/// Provides extensions for the <see cref="PagedList{T}"/> type.
	/// </summary>
	public static class PagedListExtensions
	{
		/// <summary>
		/// Returns a paged list using the given unpaged list.
		/// </summary>
		/// <typeparam name="T">The record type</typeparam>
		/// <param name="unpagedItems">The list of unpaged items</param>
		/// <param name="pageIndex">The 1-based page index</param>
		/// <param name="pageSize">The maximum number of records per page</param>
		/// <returns>A paged list</returns>
		public static PagedList<T> ToPagedList<T>(this IEnumerable<T> unpagedItems, int pageIndex, int pageSize)
		{
			return PagedList<T>.Create(unpagedItems, pageIndex, pageSize);
		}

		/// <summary>
		/// Returns a paged list using the given unpaged list.
		/// </summary>
		/// <typeparam name="T">The record type</typeparam>
		/// <param name="unpagedItems">The list of unpaged items</param>
		/// <param name="pageIndex">The 1-based page index</param>
		/// <param name="pageSize">The maximum number of records per page</param>
		/// <returns>A paged list</returns>
		public static PagedList<T> ToPagedList<T>(this IQueryable<T> unpagedItems, int pageIndex, int pageSize)
		{
			return PagedList<T>.Create(unpagedItems, pageIndex, pageSize);
		}

		/// <summary>
		/// Converts the current <see cref="IEnumerable{T}"/> data source into a paged list using the given 
		/// parameters.
		/// </summary>
		/// <typeparam name="T">The record type</typeparam>
		/// <param name="pagedItems">The paged items</param>
		/// <param name="pageIndex">The 1-based page index</param>
		/// <param name="pageSize">The maximum number of records per page</param>
		/// <param name="totalItemCount">The total number of items per page</param>
		/// <returns>A paged list</returns>
		public static PagedList<T> ToPagedList<T>(this IEnumerable<T> pagedItems, int pageIndex, int pageSize, int totalItemCount)
		{
			return (new PagedList<T>(pagedItems, pageIndex, pageSize, totalItemCount));
		}
	}
}
